package vamp_serviceweb

import org.springframework.dao.DataIntegrityViolationException

class MembreController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def geocoderService

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [membreInstanceList: Member.list(params), membreInstanceTotal: Member.count()]
    }

    def create() {
        [membreInstance: new Member(params)]
    }

    def save() {
        def membreInstance = new Member(params)

        def addressInstance = new Address(line1:  params.line1, line2 : params.line2, line3: params.line3, zipCode: params.zipCode, city: params.city, country: params.country)
        String addr = params.line1+" "+params.line2+" "+params.line3+" "+params.zipCode+" "+params.city+" "+params.country
        println("address : "+addr)
        //Récupération des coordonnée GPS via googlemap
        def result = geocoderService.reverseGeocode(addr)
        //result ==> result.lat et result.lng
        println "Latitude: ${result.lat}"
        println  "Longitude: ${result.lng}"
        def gpslocationInstance = new Gpslocation(latitude: result.lat, longitude: result.lng)
        addressInstance.gpslocation = gpslocationInstance
        def memberaddressInstance = new Memberaddress(adress: addressInstance, membre: membreInstance)
        //Ajouter d'un member addresse
        gpslocationInstance.save()
        addressInstance.save()

        if (!membreInstance.save(flush: true)) {
            render(view: "create", model: [membreInstance: membreInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'membre.label', default: 'Member'), membreInstance.id])
        redirect(action: "show", id: membreInstance.id)
    }

    def show() {
        def membreInstance = Member.get(params.id)
        if (!membreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'membre.label', default: 'Member'), params.id])
            redirect(action: "list")
            return
        }

        [membreInstance: membreInstance]
    }

    def edit() {
        def membreInstance = Member.get(params.id)
        if (!membreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'membre.label', default: 'Member'), params.id])
            redirect(action: "list")
            return
        }

        [membreInstance: membreInstance]
    }

    def update() {
        def membreInstance = Member.get(params.id)
        if (!membreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'membre.label', default: 'Member'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (membreInstance.version > version) {
                membreInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'membre.label', default: 'Member')] as Object[],
                        "Another user has updated this Member while you were editing")
                render(view: "edit", model: [membreInstance: membreInstance])
                return
            }
        }

        membreInstance.properties = params

        if (!membreInstance.save(flush: true)) {
            render(view: "edit", model: [membreInstance: membreInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'membre.label', default: 'Member'), membreInstance.id])
        redirect(action: "show", id: membreInstance.id)
    }

    def delete() {
        def membreInstance = Member.get(params.id)
        def memberaddressInstance = Memberaddress.findWhere(membre: membreInstance)
        if (!membreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'membre.label', default: 'Member'), params.id])
            redirect(action: "list")
            return
        }

        try {
            membreInstance.delete(flush: true)
            //En cascade on efface aussi l'addresse d'un membre
            memberaddressInstance.delete(flush:  true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'membre.label', default: 'Member'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'membre.label', default: 'Member'), params.id])
            redirect(action: "show", id: params.id)
        }
    }

    def login = {
        def members = Member.list()
        println("Je suis en dehor de la boucle !!")
        for (int i = 0; i < members.size(); i++){
            //println(membres.get(i).getAt("firstName"))
            println(members.get(i).firstName)
            println(params.username)
            println(params.password)
            if(params.username == members.get(i).firstName && params.password == members.get(i).password){
                flash.message = "login succedd"
                println ("login : reussi")
                //On stocke l'object
                session.member = members.get(i)
                println(session.member)
                redirect(action: "login")
                return
            }
        }
        flash.message = "login failed"
    }

    def logout ={
        session.member = null
        redirect(action:  'index')
    }

}
